<?php
class Home_model extends Model {
	function Home_model()
	{
		// call the model constructor
		parent::Model();
		$this->load->library('session');
		}
	
	function getData($tablename)
	{
		// Query the data table for every record and row
		$query = $this->db->get($tablename);
		if($query->num_rows() > 0)
		{
			return $query->result();
		}	
	}

		function getUserData($user_id = 0)
		{		
			$query_txt = "select * from free_user where n_uid = ?  ";
			$query = $this->db->query($query_txt,array($user_id));
			$userArray = array();
			if($query->num_rows() > 0){
				$result = $query->row_array();
				$userArray = array('freeid'=>$result['n_uid'],
				'usrname'=>$result['n_name']);
			}
			return $userArray;		
		}
	
	function querysiteConfig($columname)
	{
		// Query the site_config table for site specific variables
		$qry = " select configvalue from site_config where configtype = ? ";
		$query = $this->db->query($qry, $columname);
		if($query->num_rows() > 0)
		{
			$row = $query->row();
			
			return $row->configvalue;
		}	
	}

	function querysiteLinks($usrid = 0,$mainlink = 0, $cpage = '')
	{
		// Query the site_link table for main enterprise links
		
		if($usrid > 0 ){
		$qry = " select linkname, linktitle from site_link where linktype = 3 and mainlink = ? ";
		$qry.= " and (linktitle not like '%Sign Up%' and linktitle not like '%Log In%') ORDER BY linkid  ";
		
		}
		else if($cpage=='signup')
		{
		$qry = " select linkname, linktitle from site_link where linktype = 3 and mainlink = ?  ";
		$qry.= " and (linktitle not like '%log out%' and linktitle not like '%Sign Up%' and linktitle not like '%Create Groups%' and linktitle not like '%Edit profile%' and linktitle not like '%Home%' and linktitle not like '%Group Settings%' and linktitle not like '%Sent Message%' and linktitle not like '%Change Account%'  ) ORDER BY linkid ";
		}
		else if($usrid < 1){
		$qry = " select linkname, linktitle from site_link where linktype = 3 and mainlink = ?  ";
		$qry.= " and (linktitle not like '%log out%' and linktitle not like '%Log In%' and linktitle not like '%Create Groups%' and linktitle not like '%Edit profile%' and linktitle not like '%Home%' and linktitle not like '%Group Settings%'  and linktitle not like '%Sent Message%' and linktitle not like '%Change Account%' ) ORDER BY linkid ";
		
		}
		

		$query = $this->db->query($qry,array($mainlink));
		if($query->num_rows() > 0)
		{		
			$linkarray = array();	
            
			foreach($query->result() as $row){
				$linkarray[] = array('linkname'=> WEB_URL .$row->linkname,
				'linktitle'=> $row->linktitle);
			}
			return $linkarray;
		}	
	}

	function querysubLinks($mainlink = 21)
	{
		// Query the site_link table for enterprise sidebar links
		$pagename = $this->querypagename($mainlink);		
		$qry = " select linkname, linktitle from site_link where linktype = 1 and mainlink = ? ";

		$query = $this->db->query($qry,array($mainlink));
		if($query->num_rows() > 0)
		{		
			$sublinkarray = array();	
			foreach($query->result() as $row){
				$sublinkarray[] = array('linkname'=> WEB_URL . $pagename . "/" . $row->linkname,
				'linktitle'=> $row->linktitle);
			}
			return $sublinkarray;
		}	
	}	

	function querypagetitle($linkid = 21)
	{
		// Query the site_link table for main enterprise links
		$qry = " select linkname, linktitle from site_link where linktype = 1 and linkid = ? ";
		$query = $this->db->query($qry,array($linkid));
		if($query->num_rows() > 0)
		{		
			$pagearray = array();	
				$row = $query->row();
				$pagearray[] = array('pagename'=> WEB_URL .$row->linkname,
				'pagetitle'=> $row->linktitle);


			return $pagearray;
		}	
	}

	function querypagename($linkid = 1)
	{
		// returns the name of current page
		$pagename = "";
		$qry = " select linkname from site_link where linktype = 1 and linkid = ? ";
		$query = $this->db->query($qry,array($linkid));
		if($query->num_rows() > 0)
		{		
			$row = $query->row(); 
			$pagename = $row->linkname ;							
		}	
		return $pagename;		
	}

	function queryStatic($pagfreeid = 1)
	{
		// returns the name of current page
		$page_content = "";
		$qry = " select pagecontent from static_content where pagfreeid = ? ";
		$query = $this->db->query($qry,array($pagfreeid));
		if($query->num_rows() > 0)
		{		
			$row = $query->row(); 
			$page_content = $row->pagecontent ;							
		}	
		return $page_content;		
	}
	
	function queryData($query_txt)
	{
		// Query the data table for every record and row
		$query = $this->db->query($query_txt);
		if($query->num_rows() > 0)
		{
			return $query->result();
		}	
	}

	function insertData($input = array()) 
	{	
		$this->db->insert('data',$input);
		return $this->db->insert_id();		
	}
	function sendmsg($user_id,$mob,$msgtxt,$mdate,$mstat)
	{
	$msgqry="insert into freemsg_queue(fuid,msg_text,msg_number,msg_datecreated,msg_status)values($user_id,'$msgtxt',$mob,'$mdate','$mstat')";
	$this->db->query($msgqry);
	$inid=$this->db->insert_id();
	return $inid;
	}
	function updatemsg($inid,$res,$resid)
	{
	$upd="Update freemsg_queue SET msg_status = ?,response_id = ? where fmsg_id= ?";
	 $this->db->query($upd,array($res,$resid,$inid));
	 return 1 ;
	}
	function selpassword($email)
	{
	 $qry="select n_pwd from free_user where n_email= '$email'";
	 $res=$this->db->query($qry);
	 $passarray=array();
	 if($res->num_rows()>0)
	   {
	     foreach($res->result() as $row)
		 {
	     $passarray[]=array("n_pwd"=>$row->n_pwd);
		 }
	   }
	  return  $passarray;
	  
	}
	function selpass($user_id)
	{
	$sel="select * from free_user where  n_uid = ?";
	$res=$this->db->query($sel,array($user_id));
	   if($res->num_rows()>0)
	   {
	     foreach($res->result() as $row)
		 {
	     $selarray[]=array("n_pwd"=>$row->n_pwd);
		 }
	   }
	  return  $selarray;
	}
	
	function changeaccount($user_id,$npassword)
	{
	        $query2="Update free_user SET n_pwd= ? where n_uid= ? ";
			$upd2=$this->db->query($query2,array($npassword,$user_id));
			if(isset($upd2))
			{
			$x=123;
			}
			return 	$x;
	}
	function selcountry()
	{
	       $country="select * from countries";
		   $res=$this->db->query($country);
		   if($res->num_rows()>0)
		   {
			   foreach($res->result() as $row)
			   {
			    $countryval[] = array("value"=>$row->countryid,"id"=>$row->countryname);
			   }
		   }
		   return $countryval;
	}
	function selstate()
	{
	       $country="select * from tblcountry_region";
		   $res=$this->db->query($country);
		   if($res->num_rows()>0)
		   {
			   foreach($res->result() as $row)
			   {
			    $countryval[] = array("country"=>$row->default_name,"region_id"=>$row->region_id);
			   }
		   }
		   return $countryval;
	}
	
	
	/* thamizh added */

	function aboutus_content()
	{
	$this->db->select('*');
 	$this->db->from('cms_content');
 	$this->db->where('cms_id', '1');
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
	}

   function terms_content()

{
	$this->db->select('*');
 	$this->db->from('cms_content');
 	$this->db->where('cms_id', '2');
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
	}

	function disclaimer()
	{
	$this->db->select('*');
 	$this->db->from('cms_content');
 	$this->db->where('cms_id', '3');
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
	}

  function contactus()
  {
  	$this->db->select('*');
 	$this->db->from('cms_content');
 	$this->db->where('cms_id', '4');
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
  }

  function help()
  {
  	$this->db->select('*');
 	$this->db->from('cms_content');
 	$this->db->where('cms_id', '5');
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
  }

	function categorydropdown()
	{
		$qry = " select catid, catname, level, parentid from category where level = 1 and parentid = 0 ";
		$query = $this->db->query($qry);
		$catarr = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result() as $row)
			{
				$parentid = $row->catid;				
				
				$rqry = " select catid , catname, level, parentid from category where parentid = ? ";
				$rquery = $this->db->query($rqry,array($parentid));
				$subcatarr = array();
				if($rquery->num_rows() >0)
				{
					
					foreach($rquery->result() as $krow)
					{
						$parentid2 = $krow->catid;
						$sqry = " select catid , catname, level, parentid from category where parentid = ? ";
						$squery = $this->db->query($sqry,array($parentid2));
						$processcatarr = array();
						if($squery->num_rows()>0)
						{
							foreach($squery->result() as $srow)
							{
								$parentid3 = $srow->catid;
								
						$dqry = " select catid , catname, level, parentid from category where parentid = ? ";
						$dquery = $this->db->query($dqry,array($parentid3));
						$doctypecatarr = array();
						if($dquery->num_rows()>0)
						{
							foreach($dquery->result() as $drow)
							{
								$doctypecatarr[] = array("catid"=>$drow->catid,"catname"=>$drow->catname,"level"=>$drow->level,"parentid"=>$drow->parentid,"childarr"=>array());
								
							}
							
						}
								$processcatarr[] = array("catid"=>$srow->catid,"catname"=>$srow->catname,"level"=>$srow->level,"parentid"=>$srow->parentid,"childarr"=>$doctypecatarr);
								
							}
							
						}
						
								$subcatarr[] = array("catid"=>$krow->catid,"catname"=>$krow->catname,"level"=>$krow->level,"parentid"=>$krow->parentid,"childarr"=>$processcatarr);
						
					}
				}
			$catarr[] = array("catid"=>$row->catid,"catname"=>$row->catname,"level"=>$row->level,"parentid"=>$row->parentid,"childarr"=>$subcatarr);
				
			}
			
		}

		return $catarr;
	}
	
	function categorylist()
	{
		$qry = " select catid, catname, level, parentid from category where level = 1 and parentid = 0 ";
		$query = $this->db->query($qry);
		$catarr = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result() as $row)
			{
				$catid = $row->catid;
				$subcatqry = " select catid, catname, level, parentid from category where level = 2 and parentid = ? ";
				$subcatquery = $this->db->query($subcatqry,array($catid));
				$subcatarr = array();
				if($subcatquery->num_rows() > 0)
				{
					foreach($subcatquery->result() as $srow)
					{
						$subcatid = $srow->catid;
							$processqry = " select catid, catname, level, parentid from category where level = 3 and parentid = ? ";
							$processquery = $this->db->query($processqry,array($subcatid));
							$processarr = array();
							if($processquery->num_rows() > 0)
							{
								foreach($processquery->result() as $prow)
								{
									$processid = $prow->catid;
									$doctypeqry = " select catid, catname, level, parentid from category where level = 4 and parentid = ? ";
									$doctypequery = $this->db->query($doctypeqry,array($processid));
									$doctypearr = array();
									if($doctypequery->num_rows() > 0)
									{
										foreach($doctypequery->result() as $drow)
										{
											$doctypeid = $drow->catid;
											$doctypearr[] = array("catid"=>$drow->catid, "catname"=>$drow->catname);
										}		

									}						
									
									$processarr[] = array("catid"=>$prow->catid, "catname"=>$prow->catname,"doctypearr"=>$doctypearr);
								}		

							}						
						$subcatarr[] = array("catid"=>$srow->catid, "catname"=>$srow->catname,"processarr"=>$processarr);
					}		

				}
					$catarr[] = array("catid"=>$row->catid, "catname"=>$row->catname,"subcatarr"=>$subcatarr);				
			}
		}

		return $catarr;
	}	
	
}
?>